<html>
<script type="text/javascript" src="chrome_ex_oauthsimple.js"></script>
<script type="text/javascript" src="chrome_ex_oauth.js"></script>
<script type="text/javascript">
var api_key = 2479554858;
var initial_query_interval_in_mins = 3;
var query_interval = initial_query_interval_in_mins * 60 * 1000;


if (localStorage.getItem('query_interval_in_mins')) {
	query_interval = localStorage.getItem('query_interval_in_mins') * 60 * 1000;
} else {
	localStorage.setItem('query_interval_in_mins',initial_query_interval_in_mins);
}

var timeout;
var unread_api_url = 'http://api.t.sina.com.cn/statuses/unread.json';
var reset_count_api_url = 'http://api.t.sina.com.cn/statuses/reset_count.json';
var with_new_status = 1;
var get_authorized = false;
var notify_text = '';

var oauth=ChromeExOAuth.initBackgroundPage({
    'request_url': "http://api.t.sina.com.cn/oauth/request_token",
    'authorize_url': "http://api.t.sina.com.cn/oauth/authorize",
    'access_url': "http://api.t.sina.com.cn/oauth/access_token",
    'consumer_key': api_key,
    'consumer_secret': "9f1327611bb6b1d1ae784f6d05a1818e",
    'scope':'',
    'app_name': "微博弹窗"

}); 

function sendRequest(api_url,callback,request) {
	oauth.sendSignedRequest(api_url,callback,request);
}

function getBasicInfo(){
	var cred_api_url = 'http://api.t.sina.com.cn/account/verify_credentials.json'
	var request = {
		'method' : 'GET',
		'parameters':{
		}
	}
	oauth.sendSignedRequest(cred_api_url,
				function(rsp,xhr){
					//console.log(rsp);
					var ret_obj = eval('(' + rsp + ')');
					localStorage.setItem('tc_uid', ret_obj.id);
					localStorage.setItem('tc_screenname',ret_obj.screen_name);
					localStorage.setItem('tc_verified',ret_obj.verified);
					localStorage.setItem('tc_imageurl',ret_obj.profile_image_url);
					localStorage.setItem('tc_desc',ret_obj.description);
					},
					request);
}

function doAuthorize(){
	oauth.authorize(function(){
		getBasicInfo();
		checkUpdates();
		});
}

function doLogout(){
	if (oauth.hasToken()){
		oauth.clearTokens();
	}
}

function onCheckUpdates(rsp,xhr){
	///console.log(rsp);
	var ret_obj = eval('(' + rsp + ')');
	//console.log(ret_obj['followers']);
	var count = 0;
	for(var p in ret_obj){
		count += ret_obj[p];
		//console.log(ret_obj[p]);
		localStorage.setItem('tc_'+p, ret_obj[p]);
	}
	//console.log('count is ' + count);
	if (count) { 
	     showNotification();
	}
}

function checkUpdates(){
	//console.log('do updates');
	if(!oauth.hasToken()) return;
	var request = {
		'method' : 'GET',
		'parameters':{
			'with_new_status' : 0
		}
	}
	oauth.sendSignedRequest(unread_api_url,onCheckUpdates,request);
	timeout = setTimeout(checkUpdates,query_interval);
}

function clearCounts(){
	var flags = ['tc_comment_flag',
				 'tc_mention_flag',
				 'tc_dm_flag',
				 'tc_follower_flag'
				];

	for (var idx in flags) { 
		if(eval(localStorage.getItem(flags[idx]))) {
			var request = {
				'method' : 'POST',
				'parameters':{
				'type' : parseInt(idx) + 1
				}
			}	
			oauth.sendSignedRequest(reset_count_api_url,function callback(rsp,xhr){},request);
		}
	}
}

function showNotification() {
	var notification = webkitNotifications.createHTMLNotification('notification.html');
	notification.show();
	clearCounts();
}

function setQueryInterval(val){
	query_interval = val * 60 * 1000;
	localStorage.setItem('query_interval_in_mins',val);
	clearTimeout(timeout);
	timeout = setTimeout(checkUpdates,query_interval);
}

if(!oauth.hasToken()){
	localStorage.setItem('tc_comment_flag',true);
	localStorage.setItem('tc_dm_flag',true);
	localStorage.setItem('tc_follower_flag',true);
	localStorage.setItem('tc_mention_flag',true);
}
checkUpdates();

</script>
</html>